package com.dd.census.controller;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dd.census.controller.common.BaseController;
import com.dd.census.entity.SysMenu;
import com.dd.census.service.SysMenuService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.validator.ValidateUtil;

@Controller
@RequestMapping(value = "rest/sysMenu")
public class SysMenuController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysMenuController.class);

	@Autowired
	private SysMenuService sysMenuService;

	@RequestMapping("selectPage")
	public ResponseEntity<Object> selectPage(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(sysMenu);

			return this.doSuccess(request, sysMenuService.selectPage(sysMenu));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("selectName")
	public ResponseEntity<Object> selectName(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(sysMenu);

			return this.doSuccess(request, sysMenuService.selectName(sysMenu));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("selectChild")
	public ResponseEntity<Object> selectChild(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(sysMenu);

			return this.doSuccess(request, sysMenuService.selectChild(sysMenu));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResponseEntity<Object> insert(HttpServletRequest request, @RequestBody SysMenu sysMenu) {

		try {
			// 基本输入验证
			ValidateUtil.validate(sysMenu);

			// 插入数据
			sysMenu.setCreateUserId(this.getUserId(request));

			sysMenuService.insert(sysMenu);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResponseEntity<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {

			return this.doSuccess(request, sysMenuService.selectById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("update")
	public ResponseEntity<Object> update(HttpServletRequest request, @RequestBody SysMenu sysMenu) {

		try {
			// 基本输入验证
			ValidateUtil.validate(sysMenu);

			// 修改数据
			sysMenu.setUpdateUserId(this.getUserId(request));
			sysMenuService.update(sysMenu);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("delete")
	public ResponseEntity<Object> delete(HttpServletRequest request, @RequestBody SysMenu sysMenu) {

		try {
			// 基本输入验证
			ValidateUtil.validate(sysMenu);

			// 修改数据
			sysMenu.setUpdateUserId(this.getUserId(request));
			sysMenuService.delete(sysMenu);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("selectAll")
	public ResponseEntity<Object> selectAll(HttpServletRequest request, @RequestBody SysMenu sysMenu) {
		try {

			return this.doSuccess(request, sysMenuService.selectAll(this.getUserId(request)));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("selectList")
	public ResponseEntity<Object> selectList(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(sysMenu);

			return this.doSuccess(request, sysMenuService.selectList(sysMenu));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}